import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        appBar: AppBar(
          title: Text('首页'),
        ),
        body: new Center(child: GridList()),
      ),
    );
  }
}

class GridList extends StatefulWidget {
  GridList({Key key}) : super(key: key);
  @override
  _GridListState createState() => _GridListState();
}

class _GridListState extends State<GridList> {
  List<ListItem> _listItems = [];
  @override
  void initState() {
    super.initState();
    _retrieveIcons();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(1.0),
      child: GridView.builder(
        itemCount: _listItems.length,
        itemBuilder: (context, index) {
          if (index == _listItems.length - 1 && _listItems.length < 200) {
            _retrieveIcons();
          }
          // return Icon(_icons[index]);
          return ListItem();
        },
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, childAspectRatio: 1.0),
      ),
    );
  }

  void _retrieveIcons() {
    Future.delayed(Duration(microseconds: 200)).then((e) {
      setState(() {
        _listItems.addAll([ListItem(), ListItem()]);
      });
    });
  }
}

class ListItem extends StatelessWidget {
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Column(
          children: [
            Image(
              image: NetworkImage(
                "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
              ),
              width: 150.0,
            ),
            Text('456')
          ],
        ),
      ),
    );
  }
}
